package leetcode.tree_problem;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 剑指 Offer 28. 对称的二叉树【队列层序遍历解】
 * https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
 */

public class IsSymmetricFunTwo {
    public boolean isSymmetric(TreeNode root) {
        if (root == null){
            return true;
        }
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root.left);
        deque.offer(root.right);
        while (!deque.isEmpty()){
            TreeNode cur1 = deque.poll();
            TreeNode cur2 = deque.poll();
            if (cur1 == null && cur2 == null){
                continue;
            }
            if (cur1 == null || cur2 == null){
                return false;
            }
            if (cur1.val != cur2.val){
                return false;
            }
            deque.offer(cur1.left);
            deque.offer(cur2.right);

            deque.offer(cur1.right);
            deque.offer(cur2.left);

        }
        return true;
    }
}
